package com.gdssoft.pdaweb.action.water;
/*
 *  捷达世软件（深圳）有限公司
 *  Copyright (C) 2008-2010 ShenZhen JieDaShi Software Limited Company.
*/
import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.gdssoft.core.command.QueryFilter;
import com.gdssoft.core.util.BeanUtil;
import com.gdssoft.core.util.ContextUtil;
import com.gdssoft.core.web.action.BaseAction;
import com.gdssoft.pdaweb.model.water.PdaWaterMeter;
import com.gdssoft.pdaweb.service.water.PdaWaterMeterService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
/**
 * 
 * @author 
 *
 */
public class PdaWaterMeterAction extends BaseAction{
	@Resource
	private PdaWaterMeterService pdaWaterMeterService;
	private PdaWaterMeter pdaWaterMeter;
	
	private Long meterId;

	public Long getMeterId() {
		return meterId;
	}

	public void setMeterId(Long meterId) {
		this.meterId = meterId;
	}

	public PdaWaterMeter getPdaWaterMeter() {
		return pdaWaterMeter;
	}

	public void setPdaWaterMeter(PdaWaterMeter pdaWaterMeter) {
		this.pdaWaterMeter = pdaWaterMeter;
	}

	/**
	 * 显示列表
	 */
	public String list(){
		
		QueryFilter filter=new QueryFilter(getRequest());
		List<PdaWaterMeter> list= pdaWaterMeterService.getAll(filter);
		
		Type type=new TypeToken<List<PdaWaterMeter>>(){}.getType();
		StringBuffer buff = new StringBuffer("{success:true,'totalCounts':")
		.append(filter.getPagingBean().getTotalItems()).append(",result:");
		
		Gson gson=new Gson();
		buff.append(gson.toJson(list, type));
		buff.append("}");
		
		jsonString=buff.toString();
		
		return SUCCESS;
	}
//	/**
//	 * 批量删除
//	 * @return
//	 */
//	public String multiDel(){
//		
//		String[]ids=getRequest().getParameterValues("ids");
//		if(ids!=null){
//			for(String id:ids){
//				pdaWaterMeterService.remove(new Long(id));
//			}
//		}
//		
//		jsonString="{success:true}";
//		
//		return SUCCESS;
//	}
	
	/**
	 * 显示详细信息
	 * @return
	 */
	public String get(){
		PdaWaterMeter pdaWaterMeter=pdaWaterMeterService.get(meterId);
		
		Gson gson=new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
		//将数据转成JSON格式
		StringBuffer sb = new StringBuffer("{success:true,data:");
		sb.append(gson.toJson(pdaWaterMeter));
		sb.append("}");
		setJsonString(sb.toString());
		
		return SUCCESS;
	}
	/**
	 * 添加及保存操作
	 */
	public String save(){
		String opt = getRequest().getParameter("opt");
		String choose = getRequest().getParameter("choose");
		String selecteds = getRequest().getParameter("selected");
		
		if(pdaWaterMeter.getAmId()!=null){
			PdaWaterMeter orgPdaWaterMeter = pdaWaterMeterService.getByMeterId(pdaWaterMeter.getAmId());
			try{
				BeanUtil.copyNotNullProperties(orgPdaWaterMeter, pdaWaterMeter);
				pdaWaterMeter = orgPdaWaterMeter;
				pdaWaterMeter.setLasteditby(ContextUtil.getCurrentUser().getUsername());
				pdaWaterMeter.setLasteditdt(new Date().toString());
			}catch(Exception ex){
				logger.error(ex.getMessage());
			}
		}
		try {
			pdaWaterMeterService.save(pdaWaterMeter);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if(!opt.isEmpty() && opt.equals("CHANGEPLAN")){
			pdaWaterMeterService.changePlan(choose,selecteds);
		}
		setJsonString("{success:true}");
		return SUCCESS;
	}
	
	
}
